<template>
  <div class="zrjgzx-root">
    <base-table-detail :table-column="tableColumn" :form-filed="formFiled" :mid="mid" :query-list-action="queryListAction" :rules="rules"
                       :submit-data-action="submitDataAction" :submit-before-action="submitBeforeAction" :load-list-before="loadListBefore">
      <template v-slot:form-item>

        <el-form-item label="加工中心" prop="intoMachiningId">
          <el-select v-model="formFiled.intoMachiningId" placeholder="请选择" clearable size="small">
            <el-option
              v-for="dict in intoMachiningIdDict"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="转入时间" prop="intoMachiningDate">
          <el-date-picker
            size="mini"
            v-model="formFiled.intoMachiningDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="转入数量" prop="intoMachiningNumber">
          <el-input type="number" v-model="formFiled.intoMachiningNumber"></el-input>
        </el-form-item>


        <el-form-item label="转入前" prop="zrq">
          <upload-image :limit="1" v-model="formFiled.zrq"></upload-image>
        </el-form-item>
        <el-form-item label="转入过程" prop="zrgc">
          <upload-image :limit="1" v-model="formFiled.zrgc"></upload-image>
        </el-form-item>
        <el-form-item label="转入后" prop="zrh">
          <upload-image :limit="1" v-model="formFiled.zrh"></upload-image>
        </el-form-item>
      </template>
    </base-table-detail>
  </div>
</template>

<script>
  import { reactive, toRefs, onMounted, ref, watch, computed } from '@vue/composition-api'
  import baseTableDetail from '../components/baseTableDetail'
  import { machingEdit, machingList, centerList } from '@/api/industry/cpf'
  import { getDicts } from '@/api/system/dict/data'
  import uploadImage from '@/components/UploadImage'

  export default {
    name: 'zrjgzx',
    props: {
      mid: {
        type: [String, Number],
        default: 1
      }
    },
    components: {
      uploadImage,
      baseTableDetail
    },
    setup(props, content) {
      let data = reactive({
        tableColumn: [
          { name: '加工中心编号', prop: 'intoMachiningId' },
          { name: '加工中心名称', prop: 'intoMachiningName' },
          { name: '转入时间', prop: 'intoMachiningDate' },
          { name: '转入数量', prop: 'intoMachiningNumber' },
          {
            name: '操作',
            prop: 'operation',
            type: 'slot',
            width: 100
          }],
        rules: {},
        queryListAction: machingList,
        submitDataAction: machingEdit,
        formFiled: {
          intoMachiningId: null,
          intoMachiningDate: null,
          intoMachiningNumber: null,
          zrq: [],
          zrgc: [],
          zrh: []
        },
        intoMachiningIdDict: []
      })

      const loadDict = async() => {
        let response = await Promise.all([
          centerList()
        ])
        let [{ rows: intoMachiningIdDict }] = response
        data.intoMachiningIdDict = intoMachiningIdDict
      }

      const loadListBefore = async(response) => {
        await loadDict()
        response.rows.map(item => {
          if (item.intoMachiningId != null) {
            item.intoMachiningId = data.intoMachiningIdDict.find(el => item.intoMachiningId == el.id).name
          }
          return item
        })
        return response
      }

      const submitBeforeAction = (requestData) => {
        let req = JSON.parse(JSON.stringify(requestData))
        req.photos = [{ type: 1, ...req.zrq[0] }, { type: 2, ...req.zrgc[0] }, { type: 3, ...req.zrh[0] }]
        req.intoMachiningName = data.intoMachiningIdDict.find(el => req.intoMachiningId == el.id).name
        return req
      }
      onMounted(() => {
      })
      return { ...toRefs(data), submitBeforeAction, loadListBefore }
    }
  }
</script>

<style scoped lang="scss">
  .zrjgzx-root {

  }
</style>
